/***************************************************************************************************************

Dynamic Row functions

REQUIREMENTS
- Main table id should be "[section name]Table".
- Row id should be "[section name]Row_[row index]".
- Main table should have a hidden field for last index. The id should be "[section name]_last_index".
- Form fields should have id and name which end in "_[row index]".
- Remove button/image id should be "[section name]Remove_[row index]".
- Add button/image id should be "[section name]Add_[row index]".

WARNING
- Dynamic row currently cannot update index for event handler.
	Any event handler should pass its own object (this) and calculate the index from there.
- Dynamic row currently resets form fields to blank. No default values are taken into account.

***************************************************************************************************************/

var DynamicRow = {
	addRow: function(sectionName) {
		var lastElementID = $("[id^='" + sectionName + "Row_']:last").attr("id");
		var lastElementIndex = lastElementID.substring(sectionName.length + 4, lastElementID.length);
		var newIndex = parseInt($("#" + sectionName + "_last_index").val()) + 1;

		var newElements = DynamicRow.cloneRow(sectionName, lastElementIndex, newIndex);

		$("#" + sectionName + "Add_" + lastElementIndex).hide();
		$("#" + sectionName + "Table").append(newElements);
		$("#" + sectionName + "_last_index").val(newIndex);
	},

	cloneRow: function(sectionName, oldIndex, newIndex) {
		var newElements = $("#" + sectionName + "Row_" + oldIndex).clone();
		
		/* Change form field properties */
		$(newElements).find("[id$='_" + oldIndex + "']:input").each(function() {

			/* Change id and name */
			DynamicRow.changePropertyIndex(this, "id", oldIndex, newIndex);
			DynamicRow.changePropertyIndex(this, "name", oldIndex, newIndex);
			
			/* Reset values */
			DynamicRow.clearValue(this);
		});

		/* Change id of other HTML elements */
		$(newElements).find("[id$='_" + oldIndex + "']").andSelf().each(function() {
			var elementID = $(this).attr("id");
			elementID = elementID.substr(0, elementID.length - oldIndex.toString().length);
			$(this).attr("id", elementID + newIndex);
		});
		
		return newElements;
	},

	changePropertyIndex: function(thisElement, propertyType, oldIndex, newIndex) {
		var propertyValue = $(thisElement).attr(propertyType);
		propertyValue = propertyValue.substr(0, propertyValue.length - oldIndex.toString().length);
		$(thisElement).attr(propertyType, propertyValue + newIndex);
	},
	
	clearValue: function(thisElement) {
		/* Get input type */
		var inputType = $(thisElement).prop("tagName").toLowerCase();
		if (inputType == "input") {
			inputType = $(thisElement).prop("type").toLowerCase();
			if (inputType == "") {
				inputType = "text";
			}
		}

		switch (inputType) {
			case "text":
			case "textarea":
			case "hidden":
			case "file":
				$(thisElement).val("");
				break;
			case "select":
				$(thisElement).find(":selected").each(function() {
					$(thisElement).prop("selected", false);
				});
				break;
			case "radio":
			case "checkbox":
				$(thisElement).prop("checked", false);
				break;
		}
	},
	
	removeRow: function(sectionName, thisElement, recordIDFieldName) {
		var rowCount = $("[id^='" + sectionName + "Row_']").length;
		var deleteRow = false;
		var addBlankRow = false;
		var blankRowIndex = parseInt($("#" + sectionName + "_last_index").val()) + 1;
		var blankRow;

		var thisElementID = $(thisElement).attr("id");
		var thisIndex = thisElementID.substring(sectionName.length + 7, thisElementID.length);

		/* There are multiple rows, so removing one row is not an issue */
		if (rowCount > 1) {
			deleteRow = true;
		}

		/* Only one row in the table currently */
		else {
//			var recordID = $("#" + recordIDFieldName + "_" + thisIndex).val();
//
//			/* This is a record that is already stored in the database, so the row needs to be removed */
//			if (recordID != "") {
//				deleteRow = true;
//				addBlankRow = true;
//				blankRow = DynamicRow.cloneRow(sectionName, thisIndex, blankRowIndex);
//			}
//
//			/* This row has not been stored in the database, so clear the field values instead of removing the row */
//			else {
//				$("#" + sectionName + "Row_" + thisIndex).find("[id$='_" + thisIndex + "']:input").each(function() {
//					DynamicRow.clearValue(this);
//				});
//			}

			$("#" + sectionName + "Row_" + thisIndex).find("[id$='_" + thisIndex + "']:input").each(function() {
				DynamicRow.clearValue(this);
			});
		}

		if (deleteRow) {
			$("#" + sectionName + "Row_" + thisIndex).remove();
			$("[id^='" + sectionName + "Row_']:last").find("[id^='" + sectionName + "Add_']").show();
		}
		
		if (addBlankRow) {
			$("#" + sectionName + "Table").append(blankRow);
			$("#" + sectionName + "_last_index").val(blankRowIndex);
		}
	}
};